package com.ae.shield.client.block.tileEntityRenderer;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.ae.shield.common.block.tile.AssemblyTile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.items.IItemHandler;

/**
 * 用于给方块添加额外要渲染的东西
 * 此处用于在方块上渲染储存的物品
 */
public class AssemblyBlockTER extends TileEntityRenderer<AssemblyTile> {
    public AssemblyBlockTER(TileEntityRendererDispatcher dispatcher) {
        super(dispatcher);
    }

    @Override
    public void render(AssemblyTile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        itemRender(tileEntityIn, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn);
    }

    public void itemRender(AssemblyTile tileEntityIn, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        IItemHandler inventory = tileEntityIn.getInventory();
        Direction direction = tileEntityIn.getBlockState().get(BlockStateProperties.HORIZONTAL_FACING);
        byte facingRotation = 0;

        switch (direction) {
            case SOUTH:
                facingRotation = 2;
                break;
            case WEST:
                facingRotation = 3;
                break;
            case EAST:
                facingRotation = 1;
                break;
        }

        byte indexRotation = 0;//记录下标是否可被3整除，并用于记录旋转次数
        boolean indexFlag = false;

        for (int i = 0; i < inventory.getSlots() + 1; i++) {
            ItemStack stack;

            if (i == inventory.getSlots()) {
                IItemHandler out = tileEntityIn.getOut();
                stack = out.getStackInSlot(0);
            } else {
                stack = inventory.getStackInSlot(i);
            }

            if (i % 3 == 0 && i != 0) {
                indexFlag = true;
            }

            if (!stack.isEmpty()) {
                matrixStackIn.push();
                float size = 0.2f;
                float xpAngle = 90f;
                float ynAngle = 90 * (indexRotation + facingRotation);
                float yIn0 = 0.51f;
                float x = 0.6875f - 0.1875f * (i - indexRotation * 3 - 1);
                float z = 0.8515625f;
                float y = 0.57375f;

                if (stack.getItem() instanceof BlockItem) {
                    size = 0.3f;
                    xpAngle = 0f;
                    ynAngle = ynAngle + 180f;
                    yIn0 = y;
                }

                if (i == 0) {
                    x = 0.5f;
                    z = 0.5f;
                    y = yIn0;
                }

                if (i == inventory.getSlots()) {
                    x = 0.8125f;
                    z = 0.1484375f;
                    indexRotation = 0;
                }

                for (int j = 0; j < indexRotation + facingRotation; j++) {
                    float z1 = z;
                    z = x;
                    x = 1 - z1;
                }

                matrixStackIn.translate(x, y, z);
                matrixStackIn.rotate(new Quaternion(Vector3f.YN, ynAngle, true));
                matrixStackIn.scale(size, size, size);

                if (i == 0) {
                    matrixStackIn.rotate(new Quaternion(Vector3f.XP, xpAngle, true));
                }

                ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
                IBakedModel model = itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null);
                itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, model);
                matrixStackIn.pop();
            }

            if (indexFlag) {
                indexRotation++;
                indexFlag = false;
            }
        }
    }
}
